#coding:utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

from django import template
from django.db.models.aggregates import Count

from blog.models import Article, Tagsecond, Category

register = template.Library()

@register.assignment_tag
def archives():
    print '执行归档模板函数……'
    print Article.objects.dates('created_time', 'month', order='DESC')
    return Article.objects.dates('created_time', 'month', order='DESC')


@register.assignment_tag
def get_categories():
    # 记得在顶部引入 count 函数
    print '执行分类自定义过滤器'
    print Category.objects.annotate(num_posts=Count('article')).filter(num_posts__gt=0)
    return Category.objects.annotate(num_posts=Count('article')).filter(num_posts__gt=0)
